import re
import time
import unittest
from selenium.webdriver import Edge
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

'''
import os
import sys
import unittest


class TestSkipExamples(unittest.TestCase):

    @unittest.skip("无条件跳过，跳过该用例")
    def test_01(self):
        print('第1个用例')

    @unittest.skipIf(1==1, "条件跳过，当条件成立时跳过该用例")
    def test_02(self):
        print('第2个用例')

    @unittest.skipUnless(1==1, "条件跳过，当条件不成立时跳过该用例")
    def test_03(self):
        print('第3个用例')

    def test_network_request(self):
        if True:
            self.skipTest("动态跳过：运行时决定（代码方式）")

@unittest.skip("整个测试类跳过")
class TestSkippedClass(unittest.TestCase):
    def test_01(self):
        print('第5个用例')


if __name__ == '__main__':
    unittest.main()  # 显示更详细的输出'''


class PubliUi(unittest.TestCase):
    browser_port = None
    
    @classmethod
    def setUpClass(cls):
        '''browser_port是浏览器端口号传则在指定浏览窗口运行，空则创建新的浏览器(https://blog.csdn.net/qq_40761920/article/details/125867952)'''
        if cls.browser_port:
            edge = Options()
            edge.add_experimental_option("debuggerAddress", f'127.0.0.1:{cls.browser_port}')
            cls.dr = Edge(options=edge)
        else:
            cls.dr = Edge()
            cls.dr.maximize_window()

    def elements(self, loc, index=None):
        '''显示等待元素可见，timeout是超时时间，poll_frequency是间隔时间，loc是定位方法格式是（如By.ID）,index是元素索引默认None是调用element，传值则是调用elements并获取对应索引的元素'''
        try:
            wait = WebDriverWait(self.dr, timeout=10, poll_frequency=1)
            if index is None:
                elem = wait.until(EC.visibility_of_element_located(loc))
            else:
                elem = wait.until(EC.visibility_of_all_elements_located(loc))[index]
            return elem
        except:
            self.fail(f'定位不到元素：{loc},元素索引:{index}')

    def click(self, loc, index=0, type=0):
        '''type=0使用JavaScrip来触发点击即使元素不可见也可以，.click则是必须可见'''
        if type == 0:
            self.dr.execute_script("arguments[0].click();",self.elements(loc,index))
        else:
            self.elements(loc, index).click()

    def send_keys(self, loc, value, index=0):
        self.elements(loc, index).send_keys(value)

    def switch(self, index=-1):
        windows = self.dr.window_handles
        self.dr.switch_to.window(windows[index])
        time.sleep(2)

    def text(self, loc, index=0, changetype=None):
        '''获取元素字段，changetype=None返回原字段，float截取字段中数字并以浮点数返回'''
        text = self.elements(loc, index).text
        if changetype == float:
            text = re.findall(r"[-+]?\d*\.\d+|\d+", text)#匹配str中的float
            text = [float(num) for num in text]#将str转成float
            text = text[0]
        return text

    def get_url(self, loc, attribute='src', index=None):
        '''封装获取图片url,attribute是属性名一般是src或file'''
        url = self.elements(loc, index).get_attribute(attribute)
        self.assertIsNotNone(url, '获取不到图片url')
        return url